﻿/*****************************
* CLASS GASLAYER
* CREATES A TILEMAP ASSEMBLY
* SCROLLING BY ATTACH & REMOVE
* TILE ANIMATION SUPPORT
*****************************/

import com.gamepackage.tilemap.layer.*;

class com.gamepackage.tilemap.layer.ARMLayer
	implements com.gamepackage.tilemap.TileLayer, com.gamepackage.util.Loadable
{
	private var parent: MovieClip;
	private var depth: Number;
	private var map: Array;
	private var cols: Number;
	private var rows: Number;
	private var tilesize: Number;
	private var linkageId: String;
	
	private var timeline: MovieClip;

	private var clips: Array;

	private var x: Number, y: Number;
	private var ttx: Number, tty: Number;
	
	private var jx: Number;
	private var jy: Number;
	
	private var centered: Boolean = false;
	
	private var offsetX: Number;
	private var offsetY: Number;
	
	private var sync: Number;
	
	private var childCount: Number;
	private var childDepth: Number;
	private var depthSpace: Number;
	
	//-- MAP COLS --//
	private var mcl: Number;
	
	function ARMLayer( parent: MovieClip, depth: Number, map: Array, cols: Number, rows: Number, tilesize: Number, linkageId: String, centered: Boolean )
	{
		this.parent = parent;
		this.depth = depth;
		this.map = map;
		this.cols = cols;
		this.rows = rows;
		this.tilesize = tilesize;
		this.linkageId = linkageId;
		this.centered = centered;
		
		init();
	}
	
	private function init( Void ): Void
	{
		//-- CREATE TIMELINE --//
		timeline = parent.createEmptyMovieClip( linkageId , depth );
		
		depthSpace = 0;
		childCount = 0;
	}
	
	function create( Void ): Void
	{
		//-- COMPUTE JUMP OFFSET --//
		jx = ( cols + 1 ) * tilesize;
		jy = ( rows + 1 ) * tilesize;
		
		offsetX = ( centered ) ? jx / 2 - tilesize / 2 : 0;
		offsetY = ( centered ) ? jy / 2 - tilesize / 2 : 0;
		
		//-- BUILD CLIPS --//
		clips = new Array;
		
		var rx, ry;
		
		for( ry = 0 ; ry <= rows ; ry++ )
		{
			clips[ry] = new Array;
			
			for( rx = 0 ; rx <= cols ; rx++ )
			{
				clips[ry][rx] = undefined;
			}
		}
		
		mcl = map[0].length + depthSpace;
		
		childDepth = ( map.length + 1 ) * mcl;
		
		sync = 0;
		
		ttx = tty = Number.POSITIVE_INFINITY;
	}
	
	function moveTo( x: Number, y: Number ): Void
	{
		//-- COMPARE ASSEMBLY --//
		var tx: Number = int( ( this.x = x ) / tilesize );
		var ty: Number = int( ( this.y = y ) / tilesize );
		
		var dx: Number = ttx - tx;
		var dy: Number = tty - ty;
		
		var i: Number;
		
		ttx = tx;
		tty = ty;
		
		//-- STORE REFERENCES LOCAL --/
		var c = clips;
		var m = map;
		var t = timeline;

		//-- OTHER LOCALS --//
		var rx, ry;
		var mrow, crow;
		var clip: MovieClip;
		
		//-- MOVE TIMELINE --//
		t._x = -x;
		t._y = -y;
		
		var abs = Math.abs;
		
		var ani: MovieClip;
		
		sync++;
		
		if ( abs( dx ) > 1 || abs( dy ) > 1 )
		{
			//-- COMPLETE BUILD NEEDED --//
			ry = rows;
			while( ry > -1 )
			{
				crow = clips[ry--];
				rx = cols;
				while( rx > -1 )
				{
					crow[rx--].removeMovieClip();
				}
			}
			
			
			ry = rows;
			while( ry > -1 )
			{
				crow = clips[ry];
				mrow = map[ry+ty];
				
				rx = cols;
				while( rx > -1 )
				{
					if ( mrow[rx+tx] )
					{
						i = ( ry + ty ) * mcl + rx + tx;
						
						clip = crow[rx] = t.attachMovie(
							linkageId,
							i.toString(),
							i,
							{
								_x: ( tx + rx ) * tilesize - offsetX,
								_y: ( ty + ry ) * tilesize - offsetY
							}
						);
						clip.gotoAndStop( mrow[rx+tx] );
						if( ani = clip.a )
						{
							ani.gotoAndPlay( sync % ani._totalframes );
						}
					}
					rx--;
				}
				ry--;
			}
		}
		else
		{
			var mx, my, frame;
			//-- HORIZONTAL SCROLLING --//
			if ( dx < 0 )
			{
				//-- PUSH RIGHT --//
				mx = tx + cols;
				
				ry = c.length;
				
				while( --ry > -1 )
				{
					crow = c[ry];
					
					crow.shift().removeMovieClip();
					
					my = ry + ty + dy;
					
					if ( frame = m[ my ][ mx ] )
					{
						i = my * mcl + mx;
						
						crow.push( clip = t.attachMovie( linkageId , i.toString(), i , { _x: mx * tilesize - offsetX, _y: my * tilesize - offsetY } ) );
						clip.gotoAndStop( frame );
						if( ani = clip.a )
						{
							ani.gotoAndPlay( sync % ani._totalframes );
						}
					}
					else
					{
						crow.push( undefined );
					}
				}
			}
			else if ( dx > 0 )
			{
				//-- PUSH RIGHT --//
				ry = c.length;
				
				while( --ry > -1 )
				{
					crow = c[ry];
					
					crow.pop().removeMovieClip();
					
					my = ry + ty + dy;
					
					if ( frame = m[ my ][ tx ] )
					{
						i = my * mcl + tx;
						
						crow.unshift( clip = t.attachMovie( linkageId , i.toString(), i , { _x: tx * tilesize - offsetX, _y: my * tilesize - offsetY } ) );
						clip.gotoAndStop( frame );
						if( ani = clip.a )
						{
							ani.gotoAndPlay( sync % ani._totalframes );
						}
					}
					else
					{
						crow.unshift( undefined );
					}
				}
			}
			//-- VERTICAL SCROLLING --//
			//
			if ( dy < 0 )
			{
				var c0 = c[0];
				//-- PUSH DOWN --//
				c.push( crow = c.shift() );
				
				mrow = m[ my = ty + rows ];

				rx = crow.length;
				
				while( --rx > -1 )
				{
					crow[rx].removeMovieClip();
					
					mx = rx + tx;
					
					if ( frame = mrow[ mx ] )
					{
						i = my * mcl + mx;
						
						clip = c0[rx] = t.attachMovie( linkageId , i.toString(), i , { _x: mx * tilesize - offsetX, _y: my * tilesize - offsetY } );
						clip.gotoAndStop( frame );
						if( ani = clip.a )
						{
							ani.gotoAndPlay( sync % ani._totalframes );
						}
					}
				}
			}
			else if( dy > 0 )
			{
				//-- PUSH UP --//
				c.unshift( crow = c.pop() );
				
				var c0 = c[0];
				
				mrow = m[ ty ];
				
				rx = crow.length;
				
				while( --rx > -1 )
				{
					crow[rx].removeMovieClip();
					
					mx = rx + tx;
					
					if ( frame = mrow[ mx ] )
					{
						i = ty * mcl + mx;
						
						clip = c0[rx] = t.attachMovie( linkageId , i.toString(), i , { _x: mx * tilesize - offsetX, _y: ty * tilesize - offsetY } );
						clip.gotoAndStop( frame );
						if( ani = clip.a )
						{
							ani.gotoAndPlay( sync % ani._totalframes );
						}
					}
				}
			}
		}
	}
	
	function removeTile( rx: Number, ry: Number ): Void
	{
		var tx: Number = int( x / tilesize );
		var ty: Number = int( y / tilesize );
		
		//-- REMOVE THE MOVIECLIP --//
		clips[ry-ty][rx-tx].removeMovieClip();
		
		//-- CLEAR THE MAP ENTRY --//
		map[ry][rx] = 0;
	}
	
	function getChildMovieClip( Void ): MovieClip
	{
		return timeline.createEmptyMovieClip( '_child_' + childCount++ , ++childDepth );
	}
	
	function getLayerWidth( Void ): Number
	{
		return map[0].length * tilesize;
	}
	
	function getLayerHeight( Void ): Number
	{
		return map.length * tilesize;
	}
	
	function setDepthSpace( depthSpace: Number ): Void
	{
		this.depthSpace = depthSpace;
	}
	
	function getDepthSpace( Void ): Number
	{
		return depthSpace;
	}
	
	function getRowDepth( row: Number ): Number
	{
		return ( row + 1 ) * mcl;
	}
	
	function getMap( Void ): Array
	{
		return map;
	}
	
	function getX( Void ): Number
	{
		return x;
	}
	
	function getY( Void ): Number
	{
		return y;
	}
	
	function getLoaderClip( Void ): MovieClip
	{
		return timeline;
	}
}
















